﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using ManagerClinic.Modules;
    using System.Linq;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmDmPhongKham : DevExpress.XtraEditors.XtraForm
    {
        GridCheckMarksSelection gridCheckMarksSelection = null;
        IList<DmPhongKhamEntity> lstDmPhongKhamEntity = null;
        DmPhongKhamBusiness dmPhongKhamBusiness = new DmPhongKhamBusiness();
        DmKhoaBusiness objdmkhoabuss = new DmKhoaBusiness();

        public frmDmPhongKham()
        {
            InitializeComponent();
            LoadKhoa();
            gridCheckMarksSelection = new GridCheckMarksSelection(gvDmPhongKham);
        }

        void LoadKhoa()
        {
            var lstDmKhoaEntity = objdmkhoabuss.GetDmKhoaList();
            var dmKhoaEntity = new DmKhoaEntity()
            {
                MaKhoa = ConstantManagerClinic.sValueMember,
                TenKhoa = ConstantManagerClinic.sDisplayMember
            };
            lstDmKhoaEntity.Insert(0, dmKhoaEntity);

            lkKhoa.Properties.DataSource = lstDmKhoaEntity;
            lkKhoa.Properties.DisplayMember = "TenKhoa";
            lkKhoa.Properties.ValueMember = "MaKhoa";
            lkKhoa.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadData(object sender, EventArgs e)
        {
            lstDmPhongKhamEntity = dmPhongKhamBusiness.GetDmPhongKhamList();

            if (lkKhoa.EditValue.ToString() != ConstantManagerClinic.sValueMember)
                lstDmPhongKhamEntity = lstDmPhongKhamEntity.Where(n => n.MaPhongKham == lkKhoa.EditValue.ToString()).ToList();

            grdDmPhongKham.DataSource = lstDmPhongKhamEntity;
        }

        private void frmDMPhongkham_Load(object sender, EventArgs e)
        {
            Authenticate.IsAuthenticateForm(this);
            LoadData(sender, e);
        }

        private void lkKhoa_EditValueChanged(object sender, EventArgs e)
        {
            LoadData(sender, e);
        }

        DmPhongKhamEntity getDataDmDPKEntity()
        {
            if (gvDmPhongKham.RowCount > 0)
            {
                return (DmPhongKhamEntity)gvDmPhongKham.GetFocusedRow();
            }

            return null;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            var deleteObjects = gridCheckMarksSelection.selection;

            if (deleteObjects.Count <= 0)
            {
                MessageBox.Show("Bạn chưa chọn bản ghi để xóa!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            if (MessageBox.Show("Bạn chắc chắn muốn xóa dữ liệu?", ConstantManagerClinic.sTitleWarming, MessageBoxButtons.YesNo) == DialogResult.No)
            {
                gridCheckMarksSelection.ClearSelection();
                return;
            }

            foreach (DmPhongKhamEntity dm in deleteObjects)
            {
                dmPhongKhamBusiness.DeleteDmPhongKham(dm.MaPhongKham);
            }

            gridCheckMarksSelection.ClearSelection();
            LoadData(sender, e);
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            var frm = new frmCapNhatDmPhongKham((int)ConstantManagerClinic.StateInsertOrUpdate.Insert);
            frm.dmPhongKhamEvent += new EventHandler(LoadData);
            frm.ShowDialog();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            var dmDmPhongKhamEntity = getDataDmDPKEntity();
            if (dmDmPhongKhamEntity == null) return;

            var frm = new frmCapNhatDmPhongKham((int)ConstantManagerClinic.StateInsertOrUpdate.Update, dmDmPhongKhamEntity);
            frm.dmPhongKhamEvent += new EventHandler(LoadData);
            frm.ShowDialog();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gvDMPhongkham_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Handled = false;
            }
        }

        private void grdDMPhongkham_DoubleClick(object sender, EventArgs e)
        {
            if (btnSua.Enabled == false) return;
            btnSua_Click(sender, e);
        }
    }
}